/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.service;

import java.io.Serializable;
import java.util.Date;

import com.google.code.sapien.dao.DiscussionDAO;
import com.google.code.sapien.dao.ResponseDAO;
import com.google.code.sapien.model.Discussion;
import com.google.code.sapien.model.Response;
import com.google.code.sapien.model.User;
import com.google.code.sapien.util.PaginatedResult;
import com.google.inject.Inject;

/**
 * Default implementation of the {@link com.google.code.sapien.service.ResponseService}.
 * @author Adam
 * @version $Id: ResponseSapienService.java 28 2009-08-25 02:33:12Z a.ruggles $
 * 
 * Created on Feb 21, 2009 at 9:59:14 AM 
 */
public class ResponseSapienService implements ResponseService {

	/**
	 * The discussion data access object.
	 */
	private final DiscussionDAO discussionDAO;

	/**
	 * The response data access object.
	 */
	private final ResponseDAO responseDAO;

	/**
	 * Constructs a response sapien service.
	 * @param responseDAO The response data access object.
	 * @param discussionDAO The discussion data access object.
	 */
	@Inject
	public ResponseSapienService(final ResponseDAO responseDAO, final DiscussionDAO discussionDAO) {
		this.responseDAO = responseDAO;
		this.discussionDAO = discussionDAO;
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#add(java.lang.Object, com.google.code.sapien.model.User)
	 */
	public void add(final Response entity, final User creator) {
		final long timems = System.currentTimeMillis();
		entity.setDiscussion(discussionDAO.load(entity.getDiscussion().getId()));
		entity.getDiscussion().addToResponseCount();
		entity.setCreated(new Date(timems));
		entity.setCreator(creator);
		entity.setModified(new Date(timems));
		responseDAO.add(entity);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.ResponseService#getPage(Discussion, int, int)
	 */
	public PaginatedResult<Response> getPage(final Discussion discussion, final int page, final int resultsPerPage) {
		final int offset = ((page - 1) * resultsPerPage);
		return new PaginatedResult<Response>(
				responseDAO.list(discussion, "created", true, offset, resultsPerPage),
				responseDAO.getCount(discussion),
				page, resultsPerPage);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#get(java.io.Serializable)
	 */
	public Response get(final Serializable id) {
		return responseDAO.get(id);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#remove(java.lang.Object, com.google.code.sapien.model.User)
	 */
	public void remove(final Response entity, final User remover) {
		final Response response = responseDAO.load(entity.getId());
		response.getDiscussion().subtractFromResponseCount();
		responseDAO.remove(response);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#update(java.lang.Object, com.google.code.sapien.model.User)
	 */
	public Response update(final Response entity, final User updator) {
		final Response response = responseDAO.load(entity.getId());
		response.setModified(new Date());
		response.setBody(entity.getBody());
		return response;
	}

}
